package com.cloudeasy.provider.service.impl;

import com.cloudeasy.base.constant.GlobalConstant;
import com.cloudeasy.base.enums.ErrorCodeEnum;
import com.cloudeasy.provider.exceptions.OpcBizException;
import com.cloudeasy.provider.model.dto.gaode.GaodeLocation;
import com.cloudeasy.provider.service.OpcGaodeFeignApi;
import com.cloudeasy.provider.service.OpcRpcService;
import com.cloudeasy.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Create By 郭金宝 on 2019/1/14.<br>
 */
@Slf4j
@Service
public class OpcRpcServiceImpl implements OpcRpcService {
    @Resource
    private OpcGaodeFeignApi opcGaodeFeignApi;

    @Override
    public String getLocationById(String addressId) {
        try {
            Wrapper<GaodeLocation> wrapper = opcGaodeFeignApi.getLocationByIpAddr(addressId);
            if (wrapper == null) {
                throw new OpcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OpcBizException(ErrorCodeEnum.MDC10021002);
            }

            GaodeLocation result = wrapper.getResult();

            assert result != null;
            return result.getProvince().contains("市") ? result.getCity() : result.getProvince() + GlobalConstant.Symbol.SHORT_LINE + result.getCity();
        } catch (Exception e) {
            log.error("getLocationById={}", e.getMessage(), e);
        }
        return null;
    }
}
